using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Quotes
{
    public partial class frmSettings : Form
    {
        public frmSettings()
        {
            InitializeComponent();
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
            try
            {
                if (regKey.GetValue("BVBStockInfo", null) == null)
                    chkService.Checked = false;
                numRefresh.Value = InfoClass.refreshTime / 60000;
            }
            catch (ArgumentOutOfRangeException ex)
            {
                MessageBox.Show(ex.Message, "Error on numRefresh", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                regKey.Close();
            }            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //check for changes
            //refresh time
            if (numRefresh.Tag != null)
            {
                InfoClass.refreshTime = (int)(numRefresh.Value * 60000);
            }
            //language changed
            if (cmbLanguage.Tag != null)
            {
                //TODO
            }
            //Start when Windows starts
            if (chkService.Tag != null)
                Service(chkService.Checked);
            //Boss key
            if (chkBoss.Tag != null)
            {
                //TODO
            }
            //including alerts
            if (chkBossAlerts.Tag != null)
            {
                //TODO
            }
            //Run alerts after they are added
            if (chkAlerts.Tag != null)
            {
                InfoClass.runAlerts = true;
            }
            this.Close();
        }

        private void Service(bool enable)
        {
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
            try
            {
                regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (enable)
                {
                    regKey.SetValue("BVBStockInfo", Application.ExecutablePath, Microsoft.Win32.RegistryValueKind.String);
                }
                else
                {
                    regKey.DeleteValue("BVBStockInfo", false);
                }
            }
            catch (System.Security.SecurityException e)
            {
                MessageBox.Show(e.Message, "Registry Key Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (ObjectDisposedException e)
            {
                MessageBox.Show(e.Message, "Registry Key Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (UnauthorizedAccessException e)
            {
                MessageBox.Show(e.Message, "Registry Key Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (ArgumentException e)
            {
                MessageBox.Show(e.Message, "Registry Key Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                regKey.Close();
            }
        }

        #region Value, SelectedIndex, Checked Changed
        private void chkService_CheckedChanged(object sender, EventArgs e)
        {
            chkService.Tag = "changed";
        }

        private void chkBoss_CheckedChanged(object sender, EventArgs e)
        {
            chkBoss.Tag = "changed";
            if (chkBoss.Checked)
                chkBossAlerts.Enabled = true;
            else
                chkBossAlerts.Enabled = false;
        }

        private void chkAlerts_CheckedChanged(object sender, EventArgs e)
        {
            chkAlerts.Tag = "changed";
        }

        private void chkBossAlerts_CheckedChanged(object sender, EventArgs e)
        {
            chkBossAlerts.Tag = "changed";
        }

        private void numRefresh_ValueChanged(object sender, EventArgs e)
        {
            numRefresh.Tag = "changed";
        }

        private void cmbLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbLanguage.Tag = "changed";
        }
        #endregion

    }
}